﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Web.Task;
using PowerCreator.LMS.Task.Entity;

public partial class sysadmin_TaskManager_AddTaskClassUser : PowerCreator.LMS.Web.Base.BasePage
{
    PowerCreator.LMS.Web.Project.ProjectUser opProjectUser = new PowerCreator.LMS.Web.Project.ProjectUser();
    PowerCreator.LMS.Web.User.Department opDepartment = new PowerCreator.LMS.Web.User.Department();
    PowerCreator.LMS.Web.User.User opUser = new PowerCreator.LMS.Web.User.User();
    Project opProject = new Project();
    TaskClassManag objTaskClassManag = new TaskClassManag();
    TaskManag objTaskManag = new TaskManag();
    TaskClassUserManag objTaskClassUserManag = new TaskClassUserManag();
    TaskClassDepartmentManag objTaskClassDepartmentManag = new TaskClassDepartmentManag();
    MessageManag objMessageManag = new MessageManag();
    MessageUserInfoManag objMessageUserInfoManag = new MessageUserInfoManag();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.Params["TID"] != null && Request.Params["TID"].Trim() != "")
            {
                string taskID = Request.QueryString["TID"].ToString();
                ViewState["TaskID"] = taskID;

            }
            if (Request.Params["ID"] != null && Request.Params["ID"].Trim() != "")
            {

                string taskClassID = Request.QueryString["ID"].ToString();
                ViewState["TaskClassID"] = taskClassID;


            }

            pl_Department.Visible = false;
            //pl_Department.Visible = false;
            lblAddState.Text = "User";
            //rdbtn_User.Checked = true;
            
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            ViewState["OrderBy"] = "";          //排序字段
            ViewState["OrderType"] = " desc";   //排序方式
            BinderSex();

            InitPage();

            ViewState["ProjectClassUserType"] = "1";    //班级用户类型,1学生用户，2教师用户

            PageLoad();


            if (ViewState["ProjectClassUserType"].ToString() == ProjectClassTrainclUserType.Teahcer.ToString())
            {
                ddl_DepartmentClass.Enabled = false;
            }
            BinderTVDepartment();
            BinderDepartment();
            BinderDepartmentClass();
            tv_Department.Attributes.Add("onclick", "postBackByObject()"); //注册客户端事件
            //tv_Department.ShowCheckBoxes = TreeNodeTypes.None;

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    private void PageLoad()
    {
        TaskClass objTaskClass = objTaskClassManag.Load(int.Parse(ViewState["TaskClassID"].ToString()));
        ViewState["TaskID"] = objTaskClass.FKTaskID;
        ViewState["ProjectClassUserType"] = objTaskClass.TaskCsUserType;
        lbl_ClassName.Text = objTaskClass.TaskCsName;
        lbl_ClassUserCount.Text = objTaskClass.TaskCsUserCount.ToString();
        VOTask objVOTask = objTaskManag.Load(objTaskClass.FKTaskID);
        lbl_ProjectName.Text = objVOTask.TaskName;
    }
    protected void BinderSex()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "2";

        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "0";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "1";
        ddl_Sex.Items.Add(li);
        ddl_Sex.Items.Add(li0);
        ddl_Sex.Items.Add(li1);
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = "组织训练小组人员信息";//GetLanageValueByKey("lbl_AddClassUser_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Add.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnBack.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddDepartment.Text = GetLanageValueByKey("AddClassUser_rdbtn_r1", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddUser.Text = GetLanageValueByKey("AddClassUser_rdbtn_r2", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSee.Text = GetLanageValueByKey("AddClassUser_rdbtn_r3", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderDepartment()
    {
        string search = "DepartmentType=2";
        if (ViewState["ProjectClassUserType"].ToString() == "2")
        {
            search = "DepartmentType = 1 ";
        }
        DataTable dt = opDepartment.Query(search);
        ArrayList list = opDepartment.GetDepartmentTree(dt);
        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ddl_Department.Items.Insert(0, li);
    }
    private void BinderDepartmentClass()
    {
        if (!string.IsNullOrEmpty(ddl_Department.SelectedValue))
        {
            if (ddl_Department.SelectedValue.ToString() != "0")
            {
                VOTDepartment vo = opDepartment.Load(int.Parse(ddl_Department.SelectedValue.ToString()));
                string search = "DepartmentType=2 and DepartmentFatherId=" + vo.DepartmentId.ToString();
                DataTable dt = opDepartment.Query(search);
                ddl_DepartmentClass.DataSource = dt;
                ddl_DepartmentClass.DataTextField = "DepartmentName";
                ddl_DepartmentClass.DataValueField = "DepartmentId";
                ddl_DepartmentClass.DataBind();
            }
        }
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ddl_DepartmentClass.Items.Insert(0, li);
    }

    private void BinderTVDepartment()
    {
        string search = "DepartmentType = 2";

        //如果是教师用户，则只查询部门信息，不查询班级类型的部门信息
        if (ViewState["ProjectClassUserType"].ToString() == "2")
        {
            search = "DepartmentType = 1 ";
        }

        DataTable dt = opDepartment.Query(search);
        BinderTreeView(dt, ref tv_Department);
    }

    //绑定部门树
    private TreeView BinderTreeView(DataTable dt, ref TreeView tv)
    {
        InitTreeTreeView(null, dt, "0", ref tv);
        return null;
    }

    //绑定定部门树
    private void InitTreeTreeView(TreeNode pNode, DataTable dt, string parentid, ref TreeView tv)
    {
        if (dt.Rows.Count > 0)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[DepartmentFatherId]=" + parentid;
            foreach (DataRowView drv in dv)
            {
                TreeNode tempNode = new TreeNode();
                if (ViewState["ProjectClassUserType"].ToString() == "1")    //学生用户
                {
                    if (drv["DepartmentType"].ToString() == "1")
                    {
                        tempNode.ShowCheckBox = false;
                    }
                }
                else if (ViewState["ProjectClassUserType"].ToString() == "2")    //教师用户
                {
                    if (drv["DepartmentType"].ToString() == "2")
                    {
                        tempNode.ShowCheckBox = false;
                    }
                }

                if (pNode == null)
                {
                    //填充根节点
                    tempNode.Text = drv["DepartmentName"].ToString() + "&nbsp;";
                    tempNode.Value = drv["DepartmentId"].ToString();
                    tv.Nodes.Add(tempNode);
                    InitTreeTreeView(tempNode, dt, drv["DepartmentId"].ToString(), ref tv);
                }
                else
                {
                    tempNode.Text = drv["DepartmentName"].ToString();
                    tempNode.Value = drv["DepartmentId"].ToString();
                    pNode.ChildNodes.Add(tempNode);
                    InitTreeTreeView(tempNode, dt, drv["DepartmentId"].ToString(), ref tv);
                }
            }
        }
    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();

        sbSearch.Append(string.Format(" UserID not in (select UserID from VIEW_TaskUser where TaskCsID={0}) and ", ViewState["TaskClassID"].ToString()));

        if (ViewState["SearchType"].ToString() == "1")
        {
            if (!string.IsNullOrEmpty(txt_UserName.Text))
            {
                sbSearch.Append(string.Format(" UserName like '%{0}%' and ", txt_UserName.Text));
            }

            if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
            {
                if (!string.IsNullOrEmpty(txt_RealName.Text))
                {
                    sbSearch.Append(string.Format(" StudentName like '%{0}%' and ", txt_RealName.Text));
                }
                if (ddl_DepartmentClass.Items.Count != 0)
                {
                    string classid = ddl_DepartmentClass.SelectedValue.ToString();
                    if (classid != "0")
                    {
                        sbSearch.Append(string.Format(" DepartmentId = {0} and ", classid));
                    }
                }
                if (ddl_Sex.SelectedValue.ToString() != "2")
                {
                    sbSearch.Append(string.Format(" StudentSex = {0} and ", ddl_Sex.SelectedValue.ToString()));
                }
            }
            else if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Teahcer).ToString())
            {
                if (!string.IsNullOrEmpty(txt_RealName.Text))
                {
                    sbSearch.Append(string.Format(" TeadutyName like '%{0}%' and ", txt_RealName.Text));
                }
                if (ddl_Department.Items.Count != 0)
                {
                    string departmentid = ddl_Department.SelectedValue.ToString();
                    if (departmentid != "0")
                    {
                        sbSearch.Append(string.Format(" DepartmentId = {0} and ", departmentid));
                    }
                }
                if (ddl_Sex.SelectedValue.ToString() != "2")
                {
                    sbSearch.Append(string.Format(" TeacherSex = {0} and ", ddl_Sex.SelectedValue.ToString()));
                }
            }

            if (ddl_UserIscheck.SelectedValue.ToString() != "3")
            {
                sbSearch.Append(string.Format(" UserIscheck = {0} and ", ddl_UserIscheck.SelectedValue.ToString()));
            }
        }
        else if (ViewState["SearchType"].ToString() == "0")
        {
            if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
            {
                sbSearch.Append(string.Format(" ( UserName like '%{0}%' or  StudentName like '%{0}%' )", txt_SearchKey.Text));
            }
            else if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Teahcer).ToString())
            {
                sbSearch.Append(string.Format(" ( UserName like '%{0}%' or  TeadutyName like '%{0}%' )", txt_SearchKey.Text));
            }
        }

        if (sbSearch.ToString().EndsWith("and "))
            sbSearch.Remove(sbSearch.ToString().LastIndexOf("and "), 4);

        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
        {
            AspNetPager1.RecordCount = opUser.QueryVMAccountInfomationStudentCount(search);
        }
        else if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Teahcer).ToString())
        {
            AspNetPager1.RecordCount = opUser.QueryVMAccountInfomationTeacherCount(search);
        }
    }

    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        List<UserInfo> list = new List<UserInfo>();

        if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
        {
            dt = opUser.QueryVMAccountInfomaitionStudent(search, LMSPageSize, pageIndex);
        }
        else if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Teahcer).ToString())
        {
            dt = opUser.QueryVMAccountInfomaitionTeacher(search, LMSPageSize, pageIndex);
        }
        list = FileList(dt);
        rpt_User.DataSource = list;
        rpt_User.DataBind();
    }

    //填充数据
    private List<UserInfo> FileList(DataTable dt)
    {
        List<UserInfo> list = new List<UserInfo>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            UserInfo item = new UserInfo();

            item.UserID = int.Parse(dt.Rows[i]["UserId"].ToString());
            item.UserName = dt.Rows[i]["UserName"].ToString();
            item.UserIscheck = int.Parse(dt.Rows[i]["UserIscheck"].ToString());
            item.UserType = int.Parse(dt.Rows[i]["UserType"].ToString());

            //if (!string.IsNullOrEmpty(dt.Rows[i]["DepartmentId"].ToString()))
            //{
            //    item.DepartmentID = int.Parse(dt.Rows[i]["FKStudentDepartmentId"].ToString());
            //}

            if (item.UserType == 0)
            {
                item.DepartmentName = "-";
                item.DepartmentID = int.Parse(dt.Rows[i]["FKStudentDepartmentId"].ToString());
                item.DepartmentClassName = dt.Rows[i]["DepartmentName"].ToString();
                item.RealName = dt.Rows[i]["StudentName"].ToString();
                if (!string.IsNullOrEmpty(dt.Rows[i]["StudentSex"].ToString()))
                {
                    item.UserSex = opUser.GetUserSexByData(int.Parse(dt.Rows[i]["StudentSex"].ToString()), GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page));
                }
            }
            else if (item.UserType == 1)
            {
                item.DepartmentID = int.Parse(dt.Rows[i]["FKTeacherDepartmentId"].ToString());
                item.DepartmentName = dt.Rows[i]["DepartmentName"].ToString();
                item.DepartmentClassName = "-";
                item.RealName = dt.Rows[i]["TeacherName"].ToString();
                if (!string.IsNullOrEmpty(dt.Rows[i]["TeacherSex"].ToString()))
                {
                    item.UserSex = opUser.GetUserSexByData(int.Parse(dt.Rows[i]["TeacherSex"].ToString()), GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page));
                }
            }

            list.Add(item);
        }
        return list;
    }

   

    private void BinderPL()
    {
        if (lblAddState.Text=="Department")
        {
            pl_User.Visible = false;
        }
        else if (lblAddState.Text == "User")
        {
            pl_User.Visible = true;
        }
        //else if (rdbtn_ShowUser.Checked)
        //{
        //    string url = "";
        //    Script.RedirectURL(url);
        //}
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        //保存用户
        //if (rdbtn_User.Checked)
        //{
            PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();
            //锁定选中
            for (int i = 0; i < rpt_User.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
                if (cbox.Checked)
                {
                    Label lblid = (Label)rpt_User.Items[i].FindControl("lblid");

                    TaskClassUser objTaskClassUser = new TaskClassUser();
                    objTaskClassUser.FKTaskCSID = int.Parse(ViewState["TaskClassID"].ToString());
                    objTaskClassUser.FKTaskCurUserID = int.Parse(lblid.Text);
                    objTaskClassUser.TaskCurType = int.Parse(ViewState["ProjectClassUserType"].ToString());
                    objTaskClassUser.TaskCurCreateTime = DateTime.Now;
                    objTaskClassUser.FKTaskCurCreateID = cinfo.UserID;
                    objTaskClassUserManag.AddNew(objTaskClassUser);



                    
                }
            }
            PageLoad();
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, AspNetPager1.CurrentPageIndex);
        //}
    }

    private void BinderTreeView(TreeView tv, DataTable dt)
    {
        foreach (TreeNode var in tv.Nodes)
        {
            BinderTreeViewData(var, dt);
        }
    }
    private void BinderTreeViewData(TreeNode childNode, DataTable dt)
    {
        childNode.Checked = false;
        if (childNode.ChildNodes.Count != 0)
        {
            foreach (TreeNode var in childNode.ChildNodes)
            {
                BinderTreeViewData(var, dt);
            }
        }
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["FKTaskCDteptID"].ToString() == childNode.Value)
            {
                childNode.Checked = true;
            }
        }
    }

    protected void ddl_Department_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
        {
            BinderDepartmentClass();
        }
    }
    protected void rdbtn_Back_CheckedChanged(object sender, EventArgs e)
    {
        string url = "TaskClassManager.aspx?ID=" + ViewState["TaskID"].ToString();
        Script.RedirectURL(url);
    }
    protected void rpt_User_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "laddclassuser")
        {
            PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();


            TaskClassUser objTaskClassUser = new TaskClassUser();
            objTaskClassUser.FKTaskCSID = int.Parse(ViewState["TaskClassID"].ToString());
            objTaskClassUser.FKTaskCurUserID = int.Parse(e.CommandArgument.ToString());
            objTaskClassUser.TaskCurType = int.Parse(ViewState["ProjectClassUserType"].ToString());
            objTaskClassUser.TaskCurCreateTime = DateTime.Now;
            objTaskClassUser.FKTaskCurCreateID = cinfo.UserID;



            if (objTaskClassUserManag.AddNew(objTaskClassUser))
            {
                PageLoad();
                string search = GetSearch();
                BinderPager(search);
                BinderRPT(search, AspNetPager1.CurrentPageIndex);
            }
        }
    }
    protected void tv_Department_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        
        SetChildChecked(e.Node);
        //判断是否根节点
        if (e.Node.Parent != null)
        {
            SetParentChecked(e.Node);
        }
     
        //if (rdbtn_Dept.Checked)
        //{
            if (e.Node.Checked)
            {
                PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();

                TaskClassDepartment objTaskClassDepartment = new TaskClassDepartment();
                objTaskClassDepartment.FKTaskCsID = int.Parse(ViewState["TaskClassID"].ToString());
                objTaskClassDepartment.FKTaskCDteptID = int.Parse(e.Node.Value);
                objTaskClassDepartment.TaskCdtType = int.Parse(ViewState["ProjectClassUserType"].ToString());
                objTaskClassDepartment.TaskCdtCreateTime = DateTime.Now;
                objTaskClassDepartment.FKTaskCdtCreateID = cinfo.UserID;


               
                VOTDepartment voDepartment = opDepartment.Load(objTaskClassDepartment.FKTaskCDteptID);


                objTaskClassDepartmentManag.AddNew(objTaskClassDepartment);

                //if (objTaskClassDepartment.TaskCdtType == Convert.ToInt16(ProjectClassTrainclUserType.Student))
                //{
                //    if (voDepartment.DepartmentType == Convert.ToInt16(DepartmentType.DepartmentClass))
                //    {
                //        opProjectUser.AddProjectClassDepart(vo);
                //    }
                //    else
                //    {
                //        e.Node.ImageUrl = WebUtils.WebRoot + "/images/Ico_notstudy.gif";
                //        e.Node.Checked = false;
                //    }
                //}
                //else if (vo.TraincladeptUserType == Convert.ToInt16(ProjectClassTrainclUserType.Teahcer))
                //{
                //    if (voDepartment.DepartmentType == Convert.ToInt16(DepartmentType.Department))
                //    {
                //        opProjectUser.AddProjectClassDepart(vo);
                //    }
                //    else
                //    {

                //        e.Node.ImageUrl = WebUtils.WebRoot + "/images/Ico_notstudy.gif";
                //        e.Node.Checked = false;
                //    }
                //}
            }
            else
            {
                string search = string.Format("FKTaskCDteptID = {0} and FKTaskCsID={1}", e.Node.Value, ViewState["TaskClassID"].ToString());
                DataTable dt = objTaskClassDepartmentManag.Query(search);
                if (dt.Rows.Count == 1)
                {
                    objTaskClassDepartmentManag.Remove(int.Parse(dt.Rows[0]["TaskCdtID"].ToString()));
                }
            }
        //}
    }
    #region 级联父子节点的复选框状态

    /// <summary>
    /// 根据父节点状态设置子节点的状态
    /// </summary>
    /// <param name="parentNode"></param>
    private void SetChildChecked(TreeNode parentNode)
    {
        foreach (TreeNode node in parentNode.ChildNodes)
        {
            node.Checked = parentNode.Checked;
            if (node.ChildNodes.Count > 0)
            {
                SetChildChecked(node);
            }
        }
    }

    private void SetParentChecked(TreeNode childNode)
    {
        TreeNode parentNode = childNode.Parent;
        if (!parentNode.Checked && childNode.Checked)
        {
            int ichecks = 0;
            foreach (TreeNode node in parentNode.ChildNodes)
            {
                if (node.Checked)
                {
                    ichecks++;
                }
            }
            if (ichecks == parentNode.ChildNodes.Count)
            {
                if (parentNode.Parent != null)
                {
                    SetParentChecked(parentNode);
                }
            }
        }
        else if (parentNode.Checked && !childNode.Checked)
        {
            parentNode.Checked = false;
        }
    }
    #endregion
    protected void tv_Department_SelectedNodeChanged(object sender, EventArgs e)
    {

    }
    protected void rdbtn_ShowUser_CheckedChanged(object sender, EventArgs e)
    {
        string url = "TaskClassUserChecked.aspx?TaskClassID=" + ViewState["TaskClassID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }

    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rpt_User.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rpt_User.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }



    protected void btnAddUser_Click(object sender, EventArgs e)
    {
        lblAddState.Text = "User";

        pl_User.Visible = true;
        pl_Department.Visible = false;

        

        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
        //tv_Department.ShowCheckBoxes = TreeNodeTypes.None;
    }
    protected void btnAddDepartment_Click(object sender, EventArgs e)
    {
        lblAddState.Text = "Department";

        pl_User.Visible = false;
        pl_Department.Visible = true;

        //初始化己选班级
        string search = string.Format("FKTaskCsID={0}", ViewState["TaskClassID"].ToString());

        DataTable dt = objTaskClassDepartmentManag.Query(search);
        BinderTreeView(tv_Department, dt);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "TaskClassManager.aspx?ID=" + ViewState["TaskID"].ToString();
        Script.RedirectURL(url);
    }
    protected void btnSee_Click(object sender, EventArgs e)
    {
        string url = "TaskClassUserChecked.aspx?TaskClassID=" + ViewState["TaskClassID"].ToString();
        Script.RedirectURL(url);
    }
}
